/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.compomics.spestimator.files.fasta;

import static com.compomics.spestimator.files.fasta.clear.FastaCleanup.clearRedundancy;
import com.compomics.spestimator.files.fasta.digest.FastaDigester;
import static com.compomics.spestimator.files.fasta.digest.FastaDigester.digestWithEnzyme;
import com.compomics.spestimator.files.fasta.merge.FastaMerger;
import static com.compomics.spestimator.files.fasta.merge.FastaMerger.mergeFastaFiles;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.HashMap;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

/**
 *
 * @author Kenneth
 */
public class FastaUnifier {

    private static final Logger LOGGER = Logger.getLogger(FastaUnifier.class);

    public static void main(String args[]) {
        Logger.getRootLogger().setLevel(Level.DEBUG);
        try {
            //1 = merge files
            File fasta1 = new File(FastaMerger.class.getResource("/fasta1.FASTA").toURI());
            File fasta2 = new File(FastaMerger.class.getResource("/fasta2.FASTA").toURI());
            File output = new File(FastaMerger.class.getResource("/output.FASTA").toURI());
            mergeFastaFiles(fasta1, fasta2, output);
            //2 = digest
            File output_digest = new File(FastaDigester.class.getResource("/output_digested.FASTA").toURI());
            digestWithEnzyme(output, output_digest);
            //3 = clear redundancy
            File output_unique = new File(FastaDigester.class.getResource("/output_unique.FASTA").toURI());
            clearRedundancy(output_digest, output_unique);
        } catch (URISyntaxException | IOException ex) {
            LOGGER.error(ex);
        }
    }

    public static void makeUnifiedFasta(HashMap<String, String> fastaMap, File dest) throws IOException {
        File output = File.createTempFile("spes_merge", null, null);
        File output_digest = File.createTempFile("spes_digest", null, null);
        output.deleteOnExit();
        output_digest.deleteOnExit();
        //mergeFastaFiles(fasta1, fasta2, output);
        FastaMerger.mergeAndRename(fastaMap, output);
        //2 = digest
        digestWithEnzyme(output, output_digest);
        //3 = clear redundancy
        clearRedundancy(output_digest, dest);
        if (output.delete()) {
            LOGGER.info("Deleted " + output.getAbsolutePath());
        } else {
            LOGGER.error("Could not delete " + output.getAbsolutePath());
        }
        if (output_digest.delete()) {
            LOGGER.info("Deleted " + output_digest.getAbsolutePath());
        } else {
            LOGGER.error("Could not delete " + output_digest.getAbsolutePath());
        }
    }
}
